﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using System.IO;

namespace FormBase
{
    
    public partial class FPGM0010 : Form
    {
        List<TreeNode> mListCheckedNode = new List<TreeNode>();
        string msPath = @"C:\Users\honda.W7P8H61\Downloads";

        public FPGM0010()
        {
            InitializeComponent();
        }

        private void FPGM0010_Load(object sender, EventArgs e)
        {
            Text = "PGM0010-Base element";
            CProject.mApp.mAction.AppendMsgDebug("FPGM0010_Load()");

            listBox1.Items.Add("0 None");
            listBox1.Items.Add("1 T1");
            listBox1.Items.Add("2 T2");
            listBox1.Items.Add("3 T3");
            listBox1.Items.Add("4 T3");
        }

        private void btnAddTree_Click(object sender, EventArgs e)
        {
            //treeView1.CheckBoxes = true;
            treeView1.Nodes.Add("Books");
            treeView1.Nodes.Add("Papers");
            treeView1.Nodes.Add("Folders");

            treeView1.Nodes[0].Nodes.Add("Book1");
            treeView1.Nodes[0].Nodes.Add("Book2");
            treeView1.Nodes[0].Nodes.Add("Book3");

            treeView1.Nodes[1].Nodes.Add("Paper1");
            treeView1.Nodes[1].Nodes.Add("Paper2");
            treeView1.Nodes[1].Nodes.Add("Paper3");
        
        }

        private void btnClearTree_Click(object sender, EventArgs e)
        {
            treeView1.Nodes.Clear();
        }

        private void btnRemoveSelected_Click(object sender, EventArgs e)
        {
            if (treeView1.SelectedNode != null) 
                treeView1.SelectedNode.Remove();
        }

        private void btnRemoveChecked_Click(object sender, EventArgs e)
        {
            RemoveCheckedNodes(treeView1.Nodes);
        }
        void RemoveCheckedNodes(TreeNodeCollection pNodes)
        {
            if (GetCheckedNodes(treeView1.Nodes) > 0)
                foreach (TreeNode node2 in mListCheckedNode)
                    pNodes.Remove(node2);
        }
        int GetCheckedNodes(TreeNodeCollection pNodes)
        {
            foreach (TreeNode node1 in pNodes)
            {
                if (node1.Checked)
                {
                    mListCheckedNode.Add(node1);
                }
                else
                {
                    GetCheckedNodes(node1.Nodes);
                }
            }
            return mListCheckedNode.Count;
        }

        private void btnAddDir_Click(object sender, EventArgs e)
        {
            ListDirectory(treeView1, msPath);
        }

        void ListDirectory(TreeView treeView, string path)
        {
            treeView.Nodes.Clear();
            DirectoryInfo rootDirectoryInfo = new DirectoryInfo(path);

            treeView.Nodes.Add(CreateDirectoryNode(rootDirectoryInfo));
        }

        TreeNode CreateDirectoryNode(DirectoryInfo dirInfo)
        {
            TreeNode dirNode = new TreeNode(dirInfo.Name);
            foreach (var dir1 in dirInfo.GetDirectories())
                dirNode.Nodes.Add(CreateDirectoryNode(dir1));

            foreach (var file1 in dirInfo.GetFiles())
                dirNode.Nodes.Add(new TreeNode(file1.Name));

            return dirNode;

        }

        private void btnOpenSelectedFile_Click(object sender, EventArgs e)
        {

            string sTreeNodeName = treeView1.SelectedNode.ToString().Replace("TreeNode: ", string.Empty);
            MessageBox.Show(sTreeNodeName);
            System.Diagnostics.Process.Start(msPath + @"\" + sTreeNodeName);
        }

        private void chkWithCheckBox_CheckedChanged(object sender, EventArgs e)
        {
            CheckBox chk1 = sender as CheckBox;
            treeView1.CheckBoxes = chk1.Checked;
        }

        private void panel1_Paint(object sender, PaintEventArgs e)
        {

        }
    }
}
